/*
 * Powered By fanrenqij.com
 * Since 2017 - 2017
 */

package com.fanrenqiji.modules.business.wechatuser.web;


import com.fanrenqiji.common.config.GlobalSetting;
import com.fanrenqiji.common.persistence.Page;
import com.fanrenqiji.common.web.BaseController;
import com.fanrenqiji.modules.business.wechatuser.service.WeChatUserService;
import com.fanrenqiji.modules.business.wechatuser.vo.WeChatUserVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping(value = "${adminPath}/business/wechatuser")
public class WeChatUserController extends BaseController {

	private static Logger log = LoggerFactory.getLogger(WeChatUserController.class);
	@Autowired
	private WeChatUserService weChatUserService;

	@ModelAttribute("weChatUser")
	public WeChatUserVO getWeChatUser(@RequestParam(required = false) String id) {
		if (StringUtils.isNotEmpty(id)) {
			return weChatUserService.get(id);
		} else {
			return new WeChatUserVO();
		}
	}

	/**
	 * 列表
	 *
	 * @return
	 */
	@RequiresPermissions("business:wechatuser:view")
	@RequestMapping(value = {"list", ""})
	public String list(WeChatUserVO weChatUser, HttpServletRequest request,
					   HttpServletResponse response, Model model) {
		Page<WeChatUserVO> page = weChatUserService.findWeChatUserByPage(new Page<WeChatUserVO>(request, response),
				weChatUser);
		model.addAttribute("page", page);
		return "modules/business/wechatuser/wechatuserList";
	}

	/**
	 * 用户添加更新表
	 *
	 * @param model
	 * @return
	 */
	@RequiresPermissions("business:wechatuser:view")
	@RequestMapping(value = "form")
	public String form(Model model, WeChatUserVO weChatUser) {
		if (StringUtils.isNotEmpty(weChatUser.getId())) {
			model.addAttribute("weChatUser", weChatUserService.get(weChatUser.getId()));
		} else {
			model.addAttribute("weChatUser", weChatUser);
		}
		return "modules/business/wechatuser/wechatuserForm";
	}

	/**
	 * 添加或修改提交
	 *
	 * @param weChatUser
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("business:wechatuser:edit")
	@RequestMapping(value = "save")
	public String save(WeChatUserVO weChatUser, Model model,
					   RedirectAttributes redirectAttributes) {// TODO @Valid 添加后端校验
		if (!beanValidator(model, weChatUser)) {
			return form(model, weChatUser);
		}
		weChatUserService.update(weChatUser);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:" + GlobalSetting.getAdminPath()
				+ "/business/wechatuser/?repage";
	}

	/**
	 * 删除操作
	 *
	 * @return
	 */
	@RequestMapping("delete")
	@RequiresPermissions("business:wechatuser:edit")
	public String delete(String id, RedirectAttributes redirectAttributes) {
		weChatUserService.delete(id);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:" + GlobalSetting.getAdminPath() + "/business/wechatuser/";
	}

	/**
	 * 通过名称查询所有的集合
	 *
	 * @param name
	 * @param response
	 */
	@RequestMapping("name")
	@ResponseBody
	public void findwechatvobyname(String name, HttpServletResponse response) {
		// 先对response进行设置
		response.setContentType("text/html;charset=utf-8");
		// 如果名字不为空才进行下一步 如果名字为空 就直接让他返回
		// 可以先思考下 如果传过来是手机号 那么
		// 调用发送短信的接口 获取输入的值 还要设定多长时间内有效 然后就是进行校验
		// 如果都长校验通过 那么才能进行下一步
		try {
			if (StringUtils.isNoneBlank(name)) {
				List<WeChatUserVO> volist = weChatUserService.fingwechatuserbuname(name);
				// 接下来就是对list的长度进行判定
				if (volist.size() == 0) {
					// 如果长度为0 那么就说明 名字可以使用
					response.getWriter().write("名字可以使用");
				} else {
					response.getWriter().write("名字已经被占用");
				}

			} else {
				response.getWriter().write("用户名不能为空哦");

			}


		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}